package com.imu.purse.modules.trade.task;


import com.github.pagehelper.PageHelper;
import com.imu.common.utils.PhoneUtil;
import com.imu.constant.Constant;
import com.imu.purse.modules.bitcoin.YtfcoinApi;
import com.imu.purse.modules.trade.entity.FundEntity;
import com.imu.purse.modules.trade.service.FundService;
import com.imu.utils.ShortUUID;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component("userAddressTask")
public class UserAddressTask {

    private static final Log log = LogFactory.get(UserAddressTask.class);
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private FundService fundService;

    @Autowired
    private YtfcoinApi ytfcoinApi;

    public void handleData(Integer mod){
        //获取核心钱包地址
        String coreAddess = fundService.getCoreAddress();
        //获取所有用户地址
        int page = 1;
        int limit = 1000;
        FundEntity f = new FundEntity();
        f.setMod(mod);
        Date nowDate = new Date();
        while(true){
            f.setPage(page);
            f.setLimit(limit);
            PageHelper.startPage(f.getPage(), f.getLimit());
            List<FundEntity> fList = fundService.findAdressData(f);
            if(CollectionUtils.isEmpty(fList)){
                break;
            }
            String ytfPwd = null;
            for(FundEntity fundEntity : fList){
                try {
                    ytfPwd = fundEntity.getPhone()+ ShortUUID.shortUUID();
                    if(!PhoneUtil.isCPhone(fundEntity.getPhone())){
                        ytfPwd = ShortUUID.UUID();
                    }
                    //调用以太坊创建用户接口
                    String address = ytfcoinApi.createAccount(ytfPwd,coreAddess);
                    if(address != null){
                        //删除和修改之前缓存中数据之前的缓存中信息
                        if(StrUtil.isNotEmpty(fundEntity.getMoneyAddress()) && redisTemplate.opsForHash().hasKey(Constant.CORE_MONEY_USER_KEY,fundEntity.getMoneyAddress())){
                            redisTemplate.opsForHash().delete(Constant.CORE_MONEY_USER_KEY,fundEntity.getMoneyAddress());
                        }
                        redisTemplate.opsForHash().put(Constant.CORE_MONEY_USER_KEY,address,fundEntity.getUserId().toString());
                        redisTemplate.opsForHash().put(Constant.USER_CORE_MONEY_KEY,fundEntity.getUserId().toString(),coreAddess);
                        if(fundEntity.getId() == null){//代表在user表存在,在fund表不存在
                            fundEntity.setMoneyAddress(address);
                            fundEntity.setTimeCreate(nowDate);
                            fundEntity.setPassword(ytfPwd);
                            initFund(fundEntity);
                            fundService.save(fundEntity);
                        }else{
                            fundEntity.setPassword(ytfPwd);
                            fundEntity.setMoneyAddress(address);
                            fundEntity.setTimeUpdate(nowDate);
                            fundService.upAddressById(fundEntity);
                        }

                    }
                    Thread.sleep(10L);
                }catch (Exception e){
                    log.error(e);
                }
            }
            if(fList.size() < limit){
                break;
            }
            page++;
            /*if(page > 2){
                break;
            }*/
            log.error("page ===================" + page);
        }
    }

    /**
     * 初始化资金
     * @param fEntity
     */
    private void initFund(FundEntity fEntity){
        fEntity.setMoney(new BigDecimal(0));
        fEntity.setLockMoney(fEntity.getMoney());
        fEntity.setFrozenMoney(fEntity.getMoney());
        fEntity.setBj(fEntity.getMoney());
        fEntity.setBtc(fEntity.getMoney());
        fEntity.setFcg(fEntity.getMoney());
        fEntity.setHbc(fEntity.getMoney());
        fEntity.setHbb(fEntity.getMoney());
        fEntity.setIco(fEntity.getMoney());
        fEntity.setEth(fEntity.getMoney());
        fEntity.setFen(fEntity.getMoney());
        fEntity.setCowry(fEntity.getMoney());
        fEntity.setLockbj(fEntity.getMoney());
        fEntity.setLockfcg(fEntity.getMoney());
        fEntity.setLockico(fEntity.getMoney());
        fEntity.setLocketh(fEntity.getMoney());
        fEntity.setLockcowry(fEntity.getMoney());
    }
}
